package com.mall.dorm.services;

import com.mall.dorm.DormService;
import com.mall.dorm.constants.DormRetCode;
import com.mall.dorm.converter.DormConverter;
import com.mall.dorm.dal.entitys.BuildingRoom;
import com.mall.dorm.dal.entitys.OrderDetail;
import com.mall.dorm.dal.entitys.UserDormAssociate;
import com.mall.dorm.dal.entitys.UserInfo;
import com.mall.dorm.dal.persistence.BuildingRoomMapper;
import com.mall.dorm.dal.persistence.OrderDetailMapper;
import com.mall.dorm.dal.persistence.UserDormAssociateMapper;
import com.mall.dorm.dal.persistence.UserInfoMapper;
import com.mall.dorm.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname DormServiceImpl
 * @Description
 * @Date 2021/11/23 10:14
 * @Author terrance_swn
 */
@Slf4j
@Service
public class DormServiceImpl implements DormService {

    @Autowired
    BuildingRoomMapper buildingRoomMapper;
    @Autowired
    DormConverter dormConverter;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserDormAssociateMapper userDormAssociateMapper;

    /**
     * 查询空宿舍
     * @param request
     * @return
     */
    @Override
    public IQueryDormResponse queryEmptyDorm(IQueryDormRequest request) {

        IQueryDormResponse response = new IQueryDormResponse();

        try {
            //参数校验
            request.requestCheck();

            //业务逻辑
            Example buildingRoomExample = new Example(BuildingRoom.class);
            buildingRoomExample.createCriteria()
                    .andEqualTo("buildingId",request.getBuildingId())
                    .andEqualTo("gender",request.getGender())
                    .andEqualTo("isDel",0);

            List<BuildingRoom> buildingRooms = buildingRoomMapper.selectByExample(buildingRoomExample);
            if(buildingRooms == null || buildingRooms.size() == 0){
                response.setCode(DormRetCode.EMPTY_ROOM_NOT_EXIST.getCode());
                response.setMsg(DormRetCode.EMPTY_ROOM_NOT_EXIST.getMessage());
                return response;
            }

            List<QueryBuildRoomDto> availableBuildingRoom = new ArrayList<>();
            for (BuildingRoom buildingRoom : buildingRooms) {
                if(buildingRoom.getEmptyNo() - buildingRoom.getBrokenNo() >= request.getRequiredNum()){
                    QueryBuildRoomDto queryBuildRoomDto = dormConverter.buildingRoom2QueryBuildRoomDto(buildingRoom);
                    availableBuildingRoom.add(queryBuildRoomDto);
                }
            }

            if(availableBuildingRoom.size() == 0){
                response.setCode(DormRetCode.EMPTY_ROOM_NOT_EXIST.getCode());
                response.setMsg(DormRetCode.EMPTY_ROOM_NOT_EXIST.getMessage());
                return response;
            }

            //返回值处理
            response.setBuildRoomDtoList(availableBuildingRoom);
            response.setCode(DormRetCode.SUCCESS.getCode());
            response.setMsg(DormRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            log.info(e.getMessage());
            response.setCode(DormRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(DormRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    /**
     * 查询选宿舍结果
     * @param request
     * @return
     */
    @Override
    public IQueryResultResponse queryDormResult(IQueryResultRequest request) {

        IQueryResultResponse response = new IQueryResultResponse();

        try {
            //参数校验
            request.requestCheck();

            //业务逻辑
            Example userDormAssociateExample = new Example(UserDormAssociate.class);
            userDormAssociateExample.createCriteria()
                    .andEqualTo("orderId",request.getOrderId())
                    .andEqualTo("isDel",0);
            List<UserDormAssociate> userDormAssociates = userDormAssociateMapper.selectByExample(userDormAssociateExample);
            if(userDormAssociates == null || userDormAssociates.size() == 0){
                response.setCode(DormRetCode.DB_EXCEPTION.getCode());
                response.setMsg(DormRetCode.DB_EXCEPTION.getMessage());
                return response;
            }

            List<QueryResultMateDto> queryResultMateDtos = new ArrayList<>();
            for (UserDormAssociate userDormAssociate : userDormAssociates) {
                //加入所有人员信息(包括自己)
                Example example = new Example(UserInfo.class);
                example.createCriteria()
                        .andEqualTo("stuNo",userDormAssociate.getStuNo())
                        .andEqualTo("isDeleted",0);
                List<UserInfo> userInfos = userInfoMapper.selectByExample(example);
                if(userInfos == null || userInfos.size() == 0){
                    response.setCode(DormRetCode.DB_EXCEPTION.getCode());
                    response.setMsg(DormRetCode.DB_EXCEPTION.getMessage());
                    return response;
                }
                UserInfo userInfo = userInfos.get(0);
                QueryResultMateDto mateDto = new QueryResultMateDto();
                mateDto.setStuNo(userInfo.getStuNo());
                mateDto.setStuName(userInfo.getName());
                mateDto.setEmail(userInfo.getEmail());
                queryResultMateDtos.add(mateDto);
            }

            //返回值处理
            response.setRoomName(userDormAssociates.get(0).getRoomName());
            response.setResultMateDtoList(queryResultMateDtos);
            response.setCode(DormRetCode.SUCCESS.getCode());
            response.setMsg(DormRetCode.SUCCESS.getMessage());

        }catch (Exception e){
            log.info(e.getMessage());
            response.setCode(DormRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(DormRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }
}
